package com.coco.action;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.coco.base.BaseServlet;
import com.coco.model.Category;
import com.coco.model.Product;
import com.coco.utils.PageBean;

/**
 * Servlet implementation class ProductServlet
 */
public class ProductServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
       
    
	protected void showProductByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
		// TODO Auto-generated method stub
		String cid = request.getParameter("cid");
		Category category = categoryServiceImpl.findById(cid);
		String cname = category.getCname();
		int currentPage = 1;
		int pageSize = 6;
		String currentPage_ = request.getParameter("currentPage");
		if(currentPage_!=null){
			currentPage=Integer.parseInt(currentPage_);
		}
		
		PageBean pb=productServiceImpl.findProductByPage(currentPage, pageSize, Integer.parseInt(cid));
		request.setAttribute("cid", cid);
		request.setAttribute("cname", cname);
		request.setAttribute("pb", pb);
		request.getRequestDispatcher("product_list.jsp").forward(request, response);
	}

	protected void productDetail(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException{
		String cid = request.getParameter("cid");
		Category category = categoryServiceImpl.findById(cid);
		String cname = category.getCname();
		
		String pid = request.getParameter("pid");
		Product p=productServiceImpl.productDetail(pid);
		request.setAttribute("cid", cid);
		request.setAttribute("cname", cname);
		request.setAttribute("p", p);
		request.getRequestDispatcher("product_info.jsp").forward(request, response);
		
	}
	
	protected void searchProduct(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
		// TODO Auto-generated method stub
		String name = request.getParameter("name");
		int pageSize=6;
		int currentPage=1;
		String currentPage_=request.getParameter("currentPage");
		if(currentPage_!=null&&!"".equals(currentPage_)){
			currentPage=Integer.parseInt(currentPage_);
		}
		PageBean pb=productServiceImpl.findProductByPage(currentPage, pageSize, name);
		request.setAttribute("name", name);
		request.setAttribute("pb", pb);
		request.getRequestDispatcher("product_list1.jsp").forward(request, response);
		
	}
}
